/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta.freezay.database.dbEntities;

import de.swta.freezay.database.JPAController;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Christian
 */
@Entity
@Table(name = "PACKAGE")
@NamedQueries({@NamedQuery(name = "Package.findAll", query = "SELECT p FROM Package p"), @NamedQuery(name = "Package.findById", query = "SELECT p FROM Package p WHERE p.id = :id"), @NamedQuery(name = "Package.findByDiscount", query = "SELECT p FROM Package p WHERE p.discount = :discount"), @NamedQuery(name = "Package.findByValidity", query = "SELECT p FROM Package p WHERE p.validity = :validity"), @NamedQuery(name = "Package.findByName", query = "SELECT p FROM Package p WHERE p.name = :name"), @NamedQuery(name = "Package.findByCount", query = "SELECT p FROM Package p WHERE p.count = :count"), @NamedQuery(name = "Package.findByPrice", query = "SELECT p FROM Package p WHERE p.price = :price"), @NamedQuery(name = "Package.findByDescription", query = "SELECT p FROM Package p WHERE p.description = :description")})
public class Package implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;
    @Column(name = "DISCOUNT")
    private Double discount;
    @Column(name = "VALIDITY")
    @Temporal(TemporalType.TIMESTAMP)
    private Date validity;
    @Column(name = "NAME")
    private String name;
    @Column(name = "COUNT")
    private Integer count;
    @Column(name = "PRICE")
    private Double price;
    @Column(name = "DESCRIPTION")
    private String description;
    @JoinColumn(name = "CATEGORY", referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Category category;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "package1", fetch = FetchType.LAZY)
    private List<ItemPackage> itemPackageCollection;
    @OneToMany(mappedBy = "package1", fetch = FetchType.LAZY)
    private List<Reservation> reservationCollection;

    public Package() {
        this.name = "";
        this.description = "";
        this.reservationCollection = new ArrayList<Reservation>();
        this.itemPackageCollection = new ArrayList<ItemPackage>();
        this.count = new Integer(0);
        this.price = 0d;
    }

    public Package(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Double getDiscount() {
        return discount;
    }

    public void setDiscount(Double discount) {
        this.discount = discount;
    }

    public Date getValidity() {
        return validity;
    }

    public void setValidity(Date validity) {
        this.validity = validity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<ItemPackage> getItemPackageCollection() {
        return itemPackageCollection;
    }

    public void setItemPackageCollection(List<ItemPackage> itemPackageCollection) {
        this.itemPackageCollection = itemPackageCollection;
    }

    public List<Reservation> getReservationCollection() {
        return reservationCollection;
    }

    public void setReservationCollection(List<Reservation> reservationCollection) {
        this.reservationCollection = reservationCollection;
    }

    public int getMinimumCount() {

        List<Reservation> reservations = this.reservationCollection;
        int minCount = 0;

        for (Reservation res : reservations) {
            minCount += res.getPackagecount();
        }
        return minCount;
    }

    /**
     * MaxCount ist die maximale Anzahl der Pakete, die gepackt werden können.
     * Diese berechnet sich aus der Anzahl verfügbarer (hinzugefügter Items)!
     * 
     * @return
     */
    public int getMaximumCount() {
        int additionalCount = Integer.MAX_VALUE;
        List<ItemPackage> itemPackages = this.getItemPackageCollection();

        for (ItemPackage ip : itemPackages) {
            Item actItem = ip.getItem();
            int minCount = JPAController.ItemJpaController.findItem(actItem.getId()).getMinimumCount();
            int itemsAvail = actItem.getCount() - minCount;

            int actItemCount = ip.getItemCount();

            int addPackageAvail = itemsAvail / actItemCount;

            additionalCount = Math.min(additionalCount, addPackageAvail);
        }

        return (this.getCount() + additionalCount);
    }

    public int getAvailableCount() {

        List<Reservation> reservations = this.reservationCollection;
        int resCount = 0;

        for (Reservation res : reservations) {
            resCount += res.getPackagecount();
        }

        return this.count.intValue() - resCount;
    }

    public boolean containsItem(Integer itemId) {

        boolean cont = false;
        if (this.getId() != null) {
            cont = (JPAController.ItemPackageJpaController.findItemPackage(
                    new ItemPackagePK(this.getId(), itemId)) != null);
        }

        return cont;
    }

    public boolean isReserved() {
        return (this.reservationCollection.size() != 0);
    }

    public double calculatePrice() {
        double newPrice = 0d;

        List<ItemPackage> itemPackages = this.getItemPackageCollection();

        for (ItemPackage ip : itemPackages) {
            newPrice += ip.getItemCount() * ip.getItem().getPrice();
        }

        newPrice = newPrice - (newPrice * this.discount.doubleValue() / 100.0d);
        return newPrice;

    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Package)) {
            return false;
        }
        Package other = (Package) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "de.swta.freezay.database.dbEntities.Package[id=" + id + "]";
    }
}
